<?php

namespace App\Services;

use App\Models\EstateMenu;
use App\Traits\HelperTrait;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class EstateMenuService extends BaseService
{
    use HelperTrait;

    public function getMenus($isSeller = false)
    {
        $menu_model = new EstateMenu();
        //获取用户信息
        $user = Auth::user();
//        //获取用户的角色
//        $roles = $user->roles->toArray();
//        $role_id = array_column($roles, 'id');
//        $role_id = implode(',', $role_id);
//        $cache_name = 'estate_menus_cache:estate:' . $user->estate_id . ':role_id:' . $role_id;
//        if (!Cache::has($cache_name)) {
//            $user = Auth::user();
        if ($user['pid'] == 0) {
            //超级管理员
            $menus_list = $menu_model->orderBy('is_sort')->get()->toArray();
        } else {
            //普通管理员
            $user = Auth::user();
            $menus = $user->menus->toArray();
            $menu_id = array_column($menus, 'menu_id');
            $menus_list = $menu_model->whereIn('id', $menu_id)->orderBy('is_sort')->get()->toArray();
        }
        $list = $this->getChildren($menus_list);
//            Cache::set($cache_name, $list);
//        } else {
//            $list = Cache::get($cache_name);
//        }
        return $this->format($list);
    }


    public function getMenuslist($isSeller = false)
    {
        $menu_model = new EstateMenu();
        //获取用户信息
        $user = Auth::user();
//        //获取用户的角色
//        $roles = $user->roles->toArray();
//        $role_id = array_column($roles, 'id');
//        $role_id = implode(',', $role_id);
//        $cache_name = 'estate_menus_cache:estate:' . $user->estate_id . ':role_id:' . $role_id;
//        if (!Cache::has($cache_name)) {
//            $user = Auth::user();
        if ($user['pid'] == 0) {
            //超级管理员
            $menus_list = $menu_model->orderBy('is_sort')->where('id', '<>', 1)->get()->toArray();
        } else {
            //普通管理员
            $user = Auth::user();
            $menus = $user->menus->toArray();
            $menu_id = array_column($menus, 'menu_id');
            $menus_list = $menu_model->whereIn('id', $menu_id)->orderBy('is_sort')->where('id', '<>', 1)->get()->toArray();
        }
//
        $list = $this->getChildren($menus_list);
//            Cache::set($cache_name, $list);
//        } else {
//            $list = Cache::get($cache_name);
//        }
        return $this->format($list);
    }

    // 清空缓存
    public function clearCache()
    {
        $rs = Cache::forget('estate_menus_cache');
        return $this->format($rs);
    }
}
